package jjn.round1;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Jjn
 * @since 2022/9/24 00:03
 */
public class LeetCode1640_CanFormArray {
    public boolean canFormArray(int[] arr, int[][] pieces) {
        Map<Integer, Integer> map = new HashMap<>();
        int length = pieces.length;
        for (int i = 0; i < length; i++) {
            map.put(pieces[i][0], i);
        }
        for (int k = 0; k < arr.length; ) {
            int num = arr[k];
            //  Verify head of each piece.
            if (!map.containsKey(num)) {
                return false;
            }
            Integer index = map.get(num);
            for (int i = 0; i < pieces[index].length; i++) {
                if (arr[k + i] != pieces[index][i]) {
                    return false;
                }
            }
            k = k + pieces[index].length;
        }
        return true;
    }
}
